import isPlainObject from 'lodash/isPlainObject';
import { toPath } from '@/shared/utils/qsHelp';
import request from './request';
import httpMethod from './consts/httpMethod';

// eslint-disable-next-line import/no-mutable-exports
let createAPI = (method, url) => (parmas = {}, config = {}) => {
  console.log('parmas', parmas);
  const { pathParams, data } = parmas;
  const isPostOrPut = (method === httpMethod.POST || method === httpMethod.PUT);

  if (isPlainObject(pathParams)) {
    url = toPath(url, pathParams);
  }

  if (isPostOrPut) {
    config.data = data;
  } else {
    config.params = data;
  }

  return request({
    url,
    method,
    ...config
  });
};

if (__TESTING__) {
  createAPI = (_, url) => function tTequest() {return Promise.resolve(url);};
}

export default createAPI;

